Method and system for implementing an array using different data structures

ABSTRACT

Disclosed are a method and system for implementing an array data type of a programming language using various data structures. The disclosed method includes a plurality of implementations in which the array data type may be implemented. The implementations provide an efficient way to retrieve elements from the array, especially in the order they are inserted into the array. The data structures also minimize the computing resources required to manage and access the array. The disclosed technique also selects one of the many implementations based on criteria such as access pattern or size of the array.

CROSS-REFERENCE

This application is a continuation of U.S. patent application Ser. No.13/691,622, entitled “METHOD AND SYSTEM FOR IMPLEMENTING AN ARRAY USINGDIFFERENT DATA STRUCTURES,” filed on Nov. 30, 2012, which isincorporated herein by reference in its entirety.

FIELD OF INVENTION

This invention generally relates to data structures in computerprogramming languages. More specifically, the invention relates toimplementing an array data type using different data structures.

BACKGROUND

In certain programming languages, array data type is implemented using ahashtable. The hashtable is a data structure having “n” number of“slots” of memory. The array elements are stored as key-value pairs inthe slots of the hashtable. The index of the array is stored as a keyand the element at the index is stored as a value in the hashtable. Forexample, in an array $arr=array([‘a’]=1), the array element, “1” at anindex “a” is stored as a key-value pair (“a,1”) in the hashtable. Anelement of the array is stored in one of the slots of the hashtable. Theparticular slot in which the element is stored is determined by hashingthe index of the array to a particular slot. The hashing functiondetermines a slot number for a given index.

Prior array implementation techniques have limitations in the way thearray elements are stored. Certain implementation techniques do notinherently record or track the order in which the array elements areinserted into the array. So, applications that need to retrieve theelements in the order they were inserted into the array have to includetheir own logic in the application program to retrieve the elements inthe order they were inserted.

Certain prior techniques that support retrieving or tracking theelements in the order they are inserted use pointers. Data is storedinto a slot of the hashtable along with, for example, two pointers. Onepointer points to the next inserted element and another one points tothe previously inserted element. The pointers are updated as and whenthe elements are added or deleted from the hashtable. Storing pointerswith every array element consumes significant memory. Also, reading thechain of pointers to retrieve the elements in the order they areinserted consumes significant processor time.

SUMMARY

The present teaching provides a variety of methods, systems andparadigms for implementing an array data type of a programming languageusing various data structures. Certain disclosed techniques include aplurality of implementations in which the array data type may beimplemented. The implementations provide an efficient way to retrieveelements of the array, especially in the order they are inserted intothe array. The data structures also minimize the computing resourcesrequired to manage and access the array. Other disclosed techniques alsoselect one of the many implementations based on criteria includingaccess pattern or size of the array.

In an embodiment, the array is a data type in a dynamic programminglanguage, such as Personal Home Page (PHP). In other embodiments, thearray could be part of other programming languages. The array storesdata as key-value pairs, where a value is associated with a key of thekey-value pair. Also, the key acts as an index of the array. In otherembodiments, the array can store the data in other formats.

In a first implementation, the array is represented using a combinationof a first data structure and a second data structure. The first datastructure includes a plurality of contiguous memory buckets that storekey-value pairs in the order in which the key-value pairs are insertedinto the array. The second data structure includes a plurality of memoryslots that store pointers that point to the buckets of the first datastructure. Each of the slots corresponds to a key of the key-value pairsin the first data structure, and stores a pointer pointing to the bucketwhich has the value of the key. The key-value pairs may be retrieved inthe order they are inserted by iterating through the first datastructure. In an embodiment, the first data structure is an orderedcontainer having contiguous blocks of memory, and the second datastructure is a hashtable.

In a second implementation, the array is represented using a combinationof a first data structure and a second data structure. The first datastructure includes a plurality of slots of the memory that storekey-value pairs of the array. Each of the slots correspond to a key ofthe key-value pairs. The second data structure includes a plurality ofcontiguous buckets of the memory that store pointers pointing to theslots containing key-value pairs. Each of the pointers is associatedwith one of the key-value pairs. Further, the buckets store the pointersin the order in which the corresponding key-value pairs are insertedinto the array. The key value pairs may be retrieved in the order theywere inserted by iterating through the second data structure to obtainthe pointers to the slots containing the key-value pairs and thenobtaining the key-value pairs from the slots pointed to by the pointers.In an embodiment, the first data structure is a hashtable and the seconddata structure is an ordered container having contiguous blocks ofmemory.

Some embodiments of the invention have other aspects, elements,features, and steps in addition to or in place of what is describedabove. These potential additions and replacements are describedthroughout the rest of the specification.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a distributed environment in which an embodiment of theinvention may operate.

FIGS. 2A-2C collectively illustrate example implementations of an array.

FIG. 3 is a block diagram illustrating allocation of memory for an arraydata structure.

FIG. 4 is a block diagram of a system for implementing an array usingone of a plurality of implementations, according to an embodiment of thedisclosed technique.

FIG. 5 illustrates a process of implementing an array data structure foran array using a first implementation.

FIG. 6 is a flow diagram of a process for writing a key-value pair ofthe array into the array data structure implemented using the firstimplementation.

FIG. 7 is a flow diagram of a process for retrieving key-value pairs ofthe array in the order they are inserted, in the first implementation.

FIG. 8 is a flow diagram of a process for retrieving key-value pairs ofthe array in a random order in the first implementation.

FIG. 9 illustrates a process of implementing an array data structure foran array using a second implementation.

FIG. 10 is a flow diagram of a process for writing a key-value pair ofthe array into the array data structure implemented using the secondimplementation.

FIG. 11 is a flow diagram of a process for retrieving key-value pairs ofthe array in the order they are inserted, in the second implementation.

FIG. 12 is a flow diagram of a process for retrieving key-value pairs ofthe array in a random order, in the second implementation.

FIG. 13 is a flow diagram of a process for selecting one of a pluralityof implementations to implement an array data structure for an array.

FIG. 14 is a block diagram of an apparatus that may perform variousoperations, and store various information generated and/or used by suchoperations.

DETAILED DESCRIPTION

References in this description to “an embodiment”, “one embodiment”, orthe like, mean that the particular feature, function, or characteristicbeing described is included in at least one embodiment of the presentinvention. Occurrences of such phrases in this specification do notnecessarily all refer to the same embodiment, nor are they necessarilymutually exclusive.

Disclosed are a method and system for implementing an array datastructure of a programming language using a plurality ofimplementations. The implementations provide an efficient way toretrieve elements from the array, especially to retrieve them in theorder they were inserted into the array. The data structures alsominimize the computing resources required to manage and access thearray. The disclosed technique also selects one of the manyimplementations based on criteria including access pattern or size ofthe array.

In an embodiment, the array is a data type in a dynamic programminglanguage, such as Personal Home Page (PHP). In other embodiments, thearray could be part of other programming languages. The array storesdata as key-value pairs, where a value is associated with a key of thekey-value pair. Also, the key acts as an index of the array. In otherembodiments, the array can store the data in other formats.

In a first implementation, the array is represented using a combinationof a first data structure and a second data structure. The first datastructure includes a plurality of contiguous memory buckets that storekey-value pairs in the order in which the key-value pairs are insertedinto the array. The second data structure includes a plurality of memoryslots that store pointers that point to the buckets of the first datastructure. Each of the slots corresponds to a key of the key-value pairsin the first data structure, and stores a pointer pointing to the bucketwhich has the value of the key. The key-value pairs may be retrieved inthe order they are inserted by iterating through the first datastructure. In an embodiment, the first data structure is an orderedcontainer having contiguous blocks of memory, and the second datastructure is a hashtable.

In a second implementation, the array is represented, again, using acombination of a first data structure and a second data structure. Thefirst data structure includes a plurality of slots of the memory thatstore key-value pairs of the array. Each of the slots correspond to akey of the key-value pairs. The second data structure includes aplurality of contiguous buckets of the memory that store pointerspointing to the slots containing key-value pairs. Each of the pointersis associated with one of the key-value pairs. Further, the bucketsstore the pointers in the order in which the corresponding key-valuepairs are inserted into the array. The key value pairs may be retrievedin the order they were inserted by iterating through the second datastructure to obtain the pointers to the slots containing the key-valuepairs and then obtaining the key-value pairs from the slots pointed toby the pointers. In an embodiment, the first data structure is ahashtable and the second data structure is an ordered container havingcontiguous blocks of memory.

FIG. 1 is a distributed environment 100 in which an embodiment of theinvention may operate. The environment 100 includes a processor 115, amemory 120, a compilation module 110 and a runtime module 130. Thecompilation module 110 compiles source code 105 of a program to anintermediate code 125, and the runtime module 130 executes theintermediate code 125. The intermediate code 125 can be machine code orbyte code, for example.

The source code 105 can be written using a programming language, such asPHP. In other embodiments, the source code 105 can be written usingother programming languages. The source code 105 includes programminglanguage constructs such as class, function, and data types such asarray, etc. When the source code 105 is executed, the data types (andprogramming language constructs) are implemented, in the memory 120,using various data structures. The data structure used to implement adata type has a significant impact on the efficiency of the execution ofthe program. Further, a usage pattern and a property of the data typehave to be considered for implementing the data type using a particulardata structure. The array “arr” in the source code 105 may be accessedin various ways. For example, the array may be a read-only array, thevalues may be retrieved randomly, the values (or key-value pairs) may beretrieved in the order they were inserted, key-value pairs may bewritten into the array, the array may grow in size during the lifetimeof the program, etc. Accordingly, the data structure used to implementthe array is selected based at least on some of the above mentionedfactors.

FIGS. 2A-2C illustrate example implementations of an array, according toan embodiment of the disclosed technique. The example implementationsincludes a first implementation 225 of FIG. 2B and a secondimplementation 250 of FIG. 2C. An array, such as array 205 of FIG. 2A,can be implemented using one or both of the example implementations. Theexample implementations may be implemented in an environment such asenvironment 100 of FIG. 1. The array 205 can be part of a source code,such as source code 105.

The array data structure implemented using first implementation 225,includes a data structure 230 that contains a plurality (n+1) of slotsof memory, and another data structure 235 that contains a plurality(m+1) of contiguous buckets of memory. The data structure 230 can be,for example, a hashtable, and data structure 235 can be, for example,contiguous blocks of memory. (Henceforth, the data structure with aplurality of slots in the memory is referred to as “hashtable” and thedata structure with a plurality of contiguous buckets of memory isreferred to as “ordered container.”) In other embodiments, the datastructures 230 and 235 can be created using other similar datastructures.

The buckets of the ordered container 235 store key-value pairs 210 ofthe array 205 in the order in which the key-value pairs 210 are insertedinto the array 205. In an embodiment, the key or value or both may bepre-defined or dynamically computed. The slots of the hashtable 230store pointers that point to the buckets containing the key-value pairs210. Each of the slots in the hashtable 230 corresponds to one of thekeys of the key-value pairs 210. When a key-value pair is inserted intothe array 205, (a) a slot corresponding to the key is obtained from thehashtable 230, and (b) a bucket contiguous to the previously filledbucket is obtained from the ordered container 235. The key-value pair isstored in the retrieved bucket, and a pointer that points to theretrieved bucket is stored in the slot corresponding to the key.

For example, consider that a key-value pair of “a, 1” (“a” being the keyand “1” being the value) is inserted into the array 205. Also, considerthat the hashtable 230 and the ordered container 235 are null, or empty,that is, they do not contain any entries. When the key-value pair “a, 1”is inserted into the array 205, a bucket contiguous to previously filledbucket is obtained from the ordered container 235. Since there are nokey-value pairs in the ordered container 235, “bucket 0” is retrieved. Aslot corresponding to the key “a” is obtained from the hashtable 230. Inan embodiment, the slot is obtained using a hash function, on the key“a”, which evaluates the key to a number. Consider that the hashfunction evaluates “a” to “2.” So, “slot 2” is obtained from thehashtable 230. After obtaining the “bucket 0” and “slot 2”, thekey-value pair “a,1” is stored in “bucket 0” and pointer to the “bucket0” is stored in “slot 2.”

Similarly, when a subsequent key-value pair such as “b, 2” is insertedinto the array 205, a bucket contiguous to the previously filled bucket,that is, “bucket 1” (which is adjacent to the previously filled bucket“bucket 0”) is obtained from the ordered container 235, and a slot,“slot 0” which corresponds to the key “b” is obtained from the hashtable 210. The key-value pair “b, 2” is stored in “bucket 1” and apointer to the “bucket 1” is stored in “slot 0.” Accordingly, whensubsequent key-value pairs are inserted into the array 205, thekey-value pairs 210 are stored in the ordered container 235 in the orderthey are inserted into the array 205. In an embodiment, the slots of thehashtable 230 and the buckets of ordered container 235 may be allottedas and when the key-value pairs are added to the array.

Having a separate data structure, such as the ordered container 235, tostore the key-value pairs 210 in the order they are inserted into thearray enables the applications to retrieve them in the order they areinserted more efficiently and without having any additional logic. In anembodiment, retrieving key-value pairs in the order they are insertedinto an array from a data structure that stores them in the order theyare inserted is more efficient than retrieving from the data structuresthat do not store them in the order they are inserted.

In the second implementation 250 of FIG. 2B, the key-value pairs 210 arestored in slots of hashtable 255, and pointers to the slots are storedin the ordered container 260. One of the differences between the twoexample implementations 200 is that, the second implementation 250stores the pointers in the order of the key-value pairs they point toare inserted into the array, whereas the first implementation 225 storesthe key-value pairs in the order they were inserted into the array.

Consider that the hashtable 255 and ordered container 260 have noentries. When a key-value pair, such as “a, 1” is inserted into thearray 205, a slot corresponding to the key “a” is obtained from thehashtable 255. Consider that a hash function evaluates “a” to “3.” So,“slot 3” is obtained from the hashtable 255. A bucket that is contiguousto the previously filled bucket, that is, “bucket 0” (since there are noentries in the ordered container 260) is obtained. The key-value pair“a, 1” is inserted into “slot 3” of the hashtable 255 and a pointer to“slot 3” is stored in the “bucket 0” of the ordered container 260.Accordingly, when subsequent key-value pairs are added to the array 205,the key-value pairs are hashed into corresponding slots of the hashtable255, and pointers pointing to the key-value pairs are stored in theordered container 260 in the order in which the key-value pairs areinserted into the array.

The first implementation 225 and the second implementation 250 providedifferent benefits in different scenarios. For example, in a scenariowhere the key-value pairs 210 are often retrieved in the order they areinserted, using the first implementation 225 may be beneficial since thevalues may be obtained by iterating the ordered container 235. However,if the values in the array 205 are accessed on a random basis, forexample, by performing a look-up using a key, at least two reads may beperformed to retrieve the value. That is, a first read to obtain theslot corresponding to the key and a second read to obtain the key-valuepair from the bucket pointed to by the pointer in the slot. Accordingly,first implementation may consume more computing resources in scenarioswhere values are often retrieved on random basis than in the order theywere inserted into the array 205.

In the second implementation 250, a value of a key may obtained in asingle read, for example, by performing a look-up in the hashtable 255using the key. Accordingly, the second implementation 250 provides abenefit over the first implementation 225 in scenarios where the valuesare accessed randomly often. However, in scenarios where the values areoften retrieved in the order they are inserted, the secondimplementation 250 performs at least two reads—a first read to obtainthe pointer from the ordered store 260 and a second read to obtain thekey-value pair from the slot pointed to by the pointer. Accordingly, thesecond implementation 250 may not be as efficient compared to the firstimplementation 225 in scenarios where the values are retrieved in theorder they were inserted.

FIG. 3 is a block diagram illustrating allocation of memory for an arraydata structure, according to an embodiment of the disclosed technique.Memory environment 300 illustrates allocating memory for an array datastructure implementing an array, such as array 205 of FIG. 2. In anembodiment, the memory may be similar to the memory 120 of environment100 of FIG. 1. In the first implementation 225, the array data structurefor the array 205 “arr” is implemented using a combination of thehashtable 230 and the ordered container 235. The array data structure isallocated a block of memory.

In a first configuration 305, the block allotted for the array datastructure can contain both the hashtable 230 and the ordered container235 in the same block. The array data structure has a first pointer“pointer 1” that points to the hashtable 230 in the block, and a secondpointer “pointer 2” that points to the ordered container 235 in theblock. In an embodiment, the pointers stored in the slots of hashtable230 can be an offset to a bucket the pointer points to. The offset canbe represented using lesser number of bits than a pointer and thus,reduces the memory space consumed.

In a second configuration 310, the block 315 allotted for the array datastructure includes the hashtable 230 in the same block, but the orderedcontainer 235 is allocated a different block 320. The first pointer“pointer 1” points to the hashtable 230 in the block 315, and a secondpointer “pointer 2” points to the ordered container 235 in the block320. In an embodiment, the pointer stored in the slot of hashtable 230can be an offset of a bucket the pointer points to. The offset can berepresented using lesser number of bits than a pointer, which reducesthe consumption of memory space.

In a third configuration 325, the block 330 allotted for the array datastructure includes the ordered container 235 in the same block, but thehashtable 230 is allocated a different block 335. The first pointer“pointer 1” points to the hashtable 230 in the block 335, and a secondpointer “pointer 2” points to the ordered container 235 in the block330.

In a fourth configuration 340, both the hashtable 230 and the orderedcontainer 235 are allocated different blocks and outside of the block345 allotted for the array data structure. The first pointer “pointer 1”points to the hashtable 230 in the block 350, and a second pointer“pointer 2” points to the ordered container 235 in the block 355.

In an embodiment, a particular configuration is chosen based on variousfactors including, architecture of a computer system the program isexecuting in, memory allocation techniques of the operating system ofthe computer system, a size of the array 205, whether size of the array205 changes or remains the same during the execution of the program,etc.

FIG. 4 is a block diagram of a system 400 for implementing an arrayusing one of a plurality of implementations, according to an embodimentof the disclosed technique. The system 400 can be in an environment suchas environment 100 of FIG. 1. The specific implementation can be similarto a first implementation 225 or a second implementation 250 of FIGS. 2Band 2C, respectively. The system 400 includes a processor 405, a memory410, array usage tracking module 415, an implementation selection module420, a data structure creation module 425, an array management module430 and a storage system 435 that contains details regarding array usagepattern. Each of the modules co-ordinate with the processor 405 tocreate and manage the array data structure. Of course, in someembodiments arrays could be implemented during execution according to anindicated implementation, depending on the desired application.

The implementation selection module 415 selects at least one of thefirst implementation 225 and the second implementation 250 based oncriteria including (i) array usage pattern, or (ii) size of an array.The array tracking module 415 tracks the array usage pattern or size ofthe array during the execution of a program containing the array. Thearray tracking module 415 generates a report containing informationregarding the usage of the array, and stores the report in the storagesystem 435, such as a database. The array usage pattern that is trackedcan include at least one of (i) whether the array elements are oftenread in random order, (ii) whether the array elements are often read inthe order they are inserted into the array, (iii) a number ofelements/number of times an element is read from the array, (iv) whetherthe array is a read only array, (v) whether a size of the array changes(increases/decreases) during the lifetime of the array, etc.

In an embodiment, the implementation selection module 415 selects thefirst implementation 225 if a frequency of reading the elements of thearray in the order they are inserted exceeds a predefined threshold. Theimplementation selection module 415 selects the second implementation250 if at least one of (i) a frequency of reading the elements of thearray in a random order exceeds a predefined threshold, (ii) the numberof elements or number of times an element is read from the array exceedsa predefined threshold, or (iii) the size of the array increases beyonda predefined threshold etc.

The memory allocation configuration for the array data structure caninclude one of the four memory allocation configurations of FIG. 3. Inan embodiment, the size of the array helps in determining a particularmemory allocation configuration for the array data structure. Forexample, if the size of the array increases continuously, the secondconfiguration 310 may be selected in case of the first implementation225 or the third configuration 325 may be selected for secondimplementation 250.

In an embodiment, the access pattern of the array and the size of thearray can be tracked using a script that is executed along with theprogram containing the array. The script can generate a reportcontaining the details of the array access pattern and the size of thearray. Further, the access pattern may be tracked for a predefinednumber of executions of the program to determine the access pattern ofthe array more accurately.

After one of the first implementation 225 and the second implementation250 is selected by the implementation selection module 420, the datastructure creation module 425 creates the array data structure in thememory 410 based on the selected implementation. The array managementmodule 430 provides or co-ordinates the read/write access to the arraydata structure in the memory 410.

The array data structure created using either of the firstimplementation 225 or the second implementation 250 supports collisionresolution. A collision may result when two or more keys hash to asingle slot in the hashtable. The collision resolution techniquesdetermine a free slot in which the key-value pair may be stored. Thecollision resolution techniques include chaining, and probing techniquessuch as linear probing, quadratic probing, cuckoo hashing, doublehashing, etc.

FIG. 5 illustrates a process 500 of implementing an array data structurefor an array using a first implementation, according to an embodiment ofthe disclosed technique. The process 500 may be executed in an systemsuch as system 400 of FIG. 4. The first implementation can be similar tothe first implementation 225 of FIG. 2. At step 505, the data structurecreation module 425 creates a first data structure having a plurality ofcontiguous buckets of a memory (also referred to as “orderedcontainer”). The buckets in the ordered container are configured tostore the key-value pairs of the array in the order the key-value pairsare inserted into the array. For example, the first bucket contains thefirst key-value pair inserted into the array, the second bucket containsthe second key-value pair inserted into the array and so forth.

At step 510, the data structure creation module 425 creates a seconddata structure having a plurality of slots of memory (also referred toas “hashtable”). Each of the slots corresponds to one of the keys of thekey-value pairs in the ordered container, and stores a pointer to thebucket that contains a value of the key to which the slot corresponds.In an embodiment, a slot corresponding to a key may be determined usinga hash function on the key.

FIG. 6 is a flow diagram of a process 600 for writing a key-value pairof an array into the array data structure implemented using the firstimplementation, according to an embodiment of the disclosed technique.The process 600 may be executed in a system such as system 400 of FIG.4, and using the array data structure created using process 500 of FIG.5. At step 605, upon insertion of a key-value pair into the array, thearray management module 430 obtains a bucket from the ordered containerwhich is contiguous to the previously filled bucket. At step 610, thearray management module 430 obtains a slot from the hashtable whichcorresponds to the key of the key-value pair inserted into the array. Atstep 615, the array management module 430 stores the key-value pair inthe bucket. At step 620, the array management module 430 stores apointer that points to the bucket, in the slot.

FIG. 7 is a flow diagram of a process 700 for retrieving key-value pairsof an array in the order they are inserted, in the first implementation,according to an embodiment of the disclosed technique. The process 700may be executed in a system such as system 400 of FIG. 4, and using anarray data structure created using process 500 of FIG. 5. As describedin FIG. 3, the array data structure includes both the ordered containerand the hashtable. The array data structure includes pointers to thehashtable and the ordered container.

At step 705, the array management module 430 obtains a pointer to thefirst bucket of the ordered container. At step 710, the array managementmodule 430 iterates over the non-empty contiguous buckets of the orderedcontainer to retrieve the key-value pairs in the order they areinserted.

FIG. 8 is a flow diagram of a process 800 for retrieving key-value pairsof an array in a random order, in the first implementation, according toan embodiment of the disclosed technique. The process 800 may beexecuted in a system such as system 400 of FIG. 4, and using an arraydata structure created using process 500 of FIG. 5. At step 805, thearray management module 430 obtains a slot of the hashtablecorresponding to a specified key for which the value has to be obtained.In an embodiment, the array management module 430 obtains thecorresponding slot by using a hash function on the key. At step 810, thearray management module 430 obtains a pointer stored in the slot. Thepointer points to a bucket in the ordered container which has the value(or key-value pair) of the key to which the slot corresponds. At step815, the array management module 430 reads the key-value pair from thebucket pointed to by the pointer.

FIG. 9 illustrates a process 900 of implementing an array data structurefor an array using a second implementation, according to an embodimentof the disclosed technique. The process 900 may be executed in an systemsuch as system 400 of FIG. 4. The second implementation can be similarto the second implementation 250 of FIG. 2. At step 905, the datastructure creation module 425, creates a hashtable having a plurality ofslots. Each of the slots of the hashtable is configured to store one ofa plurality of key-value pairs of an array. Each of the slotscorresponds to one of the keys of the key-value pairs. In an embodiment,a slot corresponding to a key may be determined using a hash function onthe key.

At step 910, the data structure creation module 425 creates an orderedcontainer having a plurality of contiguous buckets of a memory. Each ofthe buckets in the ordered container is configured to store a pointerthat points to one of the slots of the hashtable. Further, the bucketsare configured to store the pointers in the order in which a key-valuepair pointed to by a pointer is inserted into the array. For example,the first bucket contains a pointer to the slot containing the firstkey-value pair inserted into the array, the second bucket contains apointer to the slot containing the second key-value pair inserted intothe array and so forth.

FIG. 10 is a flow diagram of a process 1000 for writing a key-value pairof an array into the array implemented using the second implementation,according to an embodiment of the disclosed technique. The process 1000may be executed in a system such as system 400 of FIG. 4, and using anarray data structure created using process 900 of FIG. 9. At step 1005,upon insertion of a key-value pair into the array, the array managementmodule 430 obtains a slot from the hashtable of the array datastructure. The slot corresponds to a key of the inserted key-value pair.At step 1010, the array management module 430 obtains a bucket which iscontiguous to a previously filled bucket from the ordered container. Atstep 1015, the key-value pair is stored in the slot of the hashtable. Atstep 1020, a pointer to the slot is stored in the bucket of the orderedcontainer.

FIG. 11 is a flow diagram of a process 1100 for retrieving key-valuepairs of an array in the order they are inserted, using the secondimplementation, according to an embodiment of the disclosed technique.The process 1100 may be executed in a system such as system 400 of FIG.4, and using an array data structure created using process 900 of FIG.9. As described in FIG. 3, the array data structure includes pointers tothe hashtable and the ordered container.

At step 1105, the array management module 430 obtains a pointer to thefirst bucket (i=0) of the ordered container. At step 1110, the arraymanagement module 430 retrieves a pointer stored in the i^(th) bucket.The pointer points to a slot of the hashtable which contains i^(th)key-value pair inserted into the array. (For example, i=0 indicates thefirst key value pair inserted into the array, i=1 indicates the secondvalue inserted into the array and so forth.) At step 1115, the arraymanagement module 430 retrieves the key-value pair stored in the slotpointed to by the pointer. At step 1120, the array management module 430determines whether specified number of key-value pairs are retrieved.Responsive to a determination that the specified number of key-valuepairs are retrieved, the process 1100 returns. On the other hand,responsive to a determination that the specified number of key-valuepairs are not retrieved, the control is transferred to step 1110, andthe array management module 430 iterates over remaining buckets of theordered container until the specified number of key-value pairs areretrieved. In an embodiment, all key-value pairs of the array may beretrieved.

FIG. 12 is a flow diagram of a process 1200 for retrieving key-valuepairs of an array in a random order, using the second implementation,according to an embodiment of the disclosed technique. The process 1200may be executed in a system such as system 400 of FIG. 4, and using anarray data structure creating using process 900 of FIG. 9. At step 1205,the array management module 430 obtains a slot of the hashtablecorresponding to a specified key for which the value has to be obtained.In an embodiment, the array management module 430 obtains thecorresponding slot by using a hash function on the key. At step 1210,the array management module 430 retrieves the key-value pair stored inthe slot of the hashtable.

FIG. 13 is a flow diagram of a process 1300 for selecting one of aplurality of implementations to implement a data structure of an array,according to an embodiment of the disclosed technique. The process 1300may be executed in a system such as system 400 of FIG. 4. At step 1305,the data structure creation module 425 receives a request to create anarray data structure for an array of a program. At step 1310, the arrayusage tracking module 415 retrieves predefined implementation selectioncriteria for selecting an implementation from the storage system 435.The predefined implementation selection criteria include at least one of(a) array usage pattern, or (b) the size of the array. At determinationstep 1315, the implementation module 420 determines whether a firstimplementation or a second implementation is suitable for the arraybased on the implementation selection criteria. At step 1320, responsiveto a determination that the first implementation is suitable, the firstimplementation is selected. On the other hand, responsive to adetermination that the second implementation is suitable, at step 1325,the second implementation is selected. At step 1330, the data structurecreation module 425 creates the array data structure based on theselected implementation.

FIG. 14 is a block diagram of an apparatus that may perform variousoperations, and store various information generated and/or used by suchoperations, according to an embodiment of the disclosed technique. Theapparatus can represent any computer or processing system describedherein. The processing system 1400 is a hardware device on which any ofthe entities, components or services depicted in the examples of FIGS.1-13 (and any other components described in this specification) can beimplemented, such as a compilation module, runtime module, memory, BLOB,implementation selection module, array usage tracking module, datastructure creation module, storage system, etc. The processing system1400 includes one or more processors 1405 and memory 1410 coupled to aninterconnect 1415. The interconnect 1415 is shown in FIG. 14 as anabstraction that represents any one or more separate physical buses,point to point connections, or both connected by appropriate bridges,adapters, or controllers. The interconnect 1415, therefore, may include,for example, a system bus, a Peripheral Component Interconnect (PCI) busor PCI-Express bus, a HyperTransport or industry standard architecture(ISA) bus, a small computer system interface (SCSI) bus, a universalserial bus (USB), IIC (I2C) bus, or an Institute of Electrical andElectronics Engineers (IEEE) standard 1394 bus, also called “Firewire”.

The processor(s) 1405 is/are the central processing unit (CPU) of theprocessing system 1400 and, thus, control the overall operation of theprocessing system 1400. In certain embodiments, the processor(s) 1405accomplish this by executing software or firmware stored in memory 1410.The processor(s) 1405 may be, or may include, one or more programmablegeneral-purpose or special-purpose microprocessors, digital signalprocessors (DSPs), programmable controllers, application specificintegrated circuits (ASICs), programmable logic devices (PLDs), trustedplatform modules (TPMs), or the like, or a combination of such devices.

The memory 1410 is or includes the main memory of the processing system1400. The memory 1410 represents any form of random access memory (RAM),read-only memory (ROM), flash memory, or the like, or a combination ofsuch devices. In use, the memory 1410 may contain a code. In oneembodiment, the code includes a general programming module configured torecognize the general-purpose program received via the computer businterface, and prepare the general-purpose program for execution at theprocessor. In another embodiment, the general programming module may beimplemented using hardware circuitry such as ASICs, PLDs, orfield-programmable gate arrays (FPGAs).

Also connected to the processor(s) 1405 through the interconnect 1415are a network adapter 1430, a storage device(s) 1420 and I/O device(s)1425. The network adapter 1430 provides the processing system 1400 withthe ability to communicate with remote devices, over a network and maybe, for example, an Ethernet adapter or Fibre Channel adapter. Thenetwork adapter 1430 may also provide the processing system 1400 withthe ability to communicate with other computers within the cluster. Insome embodiments, the processing system 1400 may use more than onenetwork adapter to deal with the communications within and outside ofthe cluster separately.

The I/O device(s) 1425 can include, for example, a keyboard, a mouse orother pointing device, disk drives, printers, a scanner, and other inputand/or output devices, including a display device. The display devicecan include, for example, a cathode ray tube (CRT), liquid crystaldisplay (LCD), or some other applicable known or convenient displaydevice.

The code stored in memory 1410 can be implemented as software and/orfirmware to program the processor(s) 1405 to carry out actions describedabove. In certain embodiments, such software or firmware may beinitially provided to the processing system 1400 by downloading it froma remote system through the processing system 1400 (e.g., via networkadapter 1430).

The techniques introduced herein can be implemented by, for example,programmable circuitry (e.g., one or more microprocessors) programmedwith software and/or firmware, or entirely in special-purpose hardwired(non-programmable) circuitry, or in a combination of such forms.Special-purpose hardwired circuitry may be in the form of, for example,one or more ASICs, PLDs, FPGAs, etc.

Software or firmware for use in implementing the techniques introducedhere may be stored on a machine-readable storage medium and may beexecuted by one or more general-purpose or special-purpose programmablemicroprocessors. A “machine-readable storage medium”, as the term isused herein, includes any mechanism that can store information in a formaccessible by a machine.

A machine can also be a server computer, a client computer, a personalcomputer (PC), a tablet PC, a laptop computer, a set-top box (STB), apersonal digital assistant (PDA), a cellular telephone, an iPhone, aBlackberry, a processor, a telephone, a web appliance, a network router,switch or bridge, or any machine capable of executing a set ofinstructions (sequential or otherwise) that specify actions to be takenby that machine.

A machine-accessible storage medium or a storage device(s) 1420includes, for example, recordable/non-recordable media (e.g., ROM; RAM;magnetic disk storage media; optical storage media; flash memorydevices; etc.), etc., or any combination thereof. The storage mediumtypically may be non-transitory or include a non-transitory device. Inthis context, a non-transitory storage medium may include a device thatis tangible, meaning that the device has a concrete physical form,although the device may change its physical state. Thus, for example,non-transitory refers to a device remaining tangible despite this changein state.

The term “logic”, as used herein, can include, for example, programmablecircuitry programmed with specific software and/or firmware,special-purpose hardwired circuitry, or a combination thereof.

What is claimed is:
 1. A method, comprising: creating, in a memory of acomputer system, a first data structure having multiple contiguousbuckets of the memory, the buckets configured to store multiplekey-value pairs in the order in which the key-value pairs are insertedinto an array; and creating, in the memory of the computer system, asecond data structure having multiple slots of the memory, a specifiedslot of the slots corresponding to a specified key of the key-valuepairs, the specified slot configured to store a specified pointer thatpoints to a specified bucket of the first data structure storing a valueof the specified key, the first data structure and the second datastructure being different data structures, wherein the first datastructure and the second data structure together form a combined datastructure that represents the array, and wherein the combined datastructure includes a first pointer to the first data structure and asecond pointer to the second data structure.
 2. The method of claim 1further comprising: obtaining, upon insertion of a first key-value pairinto the array, a first bucket from the first data structure, the firstbucket being contiguous in the memory to a previously filled bucket, anda first slot from the second data structure, the first slotcorresponding to a first key of the first key-value pair; storing, inthe first bucket, the first key-value pair; and storing, in the firstslot, a pointer that points to the first bucket.
 3. The method of claim2, wherein the second data structure is a hashtable.
 4. The method ofclaim 3, wherein obtaining, from the second data structure, the firstslot corresponding to the first key includes obtaining the first slot byusing a hash function on the first key.
 5. The method of claim 1,wherein the specified pointer that points to the specified bucket of thefirst data structure is an offset of the specified bucket in the firstdata structure.
 6. The method of claim 1 further comprising: reading thebuckets of the first data structure contiguously to obtain the key-valuepairs in the order the key-value pairs are inserted into the array. 7.The method of claim 1, wherein the first data structure and the seconddata structure are created in a single contiguous block of the memory.8. The method of claim 1, wherein at least one of the first datastructure or the second data structure are created in separate blocks ofthe memory.
 9. A computer-readable storage medium storingcomputer-readable instructions, the instructions comprising:instructions for creating, in a memory of a computer system, a firstdata structure having multiple slots of the memory, the slots configuredto store key-value pairs of an array, and a specified slot of the slotscorresponding to a specified key of the key-value pairs; andinstructions for creating, in the memory of the computer system, asecond data structure having multiple contiguous buckets of the memory,the buckets configured to store pointers pointing to the slots storingthe key-value pairs, a specified pointer of the pointers associated witha specified key-value pair of the key-value pairs, and the bucketsfurther configured to store the pointers in the order in which thekey-value pairs with which the pointers are associated are inserted intothe array.
 10. The computer-readable storage medium of claim 9 furthercomprising: instructions for obtaining, upon insertion of a firstkey-value pair into the array: a first slot from the first datastructure corresponding to a first key of the first key-value pair, anda first bucket from the second data structure to store a first pointerthat points to the first slot, the first bucket being contiguous in thememory to a previously filled bucket; instructions for storing, in thefirst slot, the first key-value pair; and instructions for storing, inthe first bucket, the first pointer.
 11. The computer-readable storagemedium of claim 10, wherein the first data structure is a hashtable. 12.The computer-readable storage medium of claim 11, wherein theinstructions for obtaining, from the first data structure, the firstslot corresponding to the first key includes instructions for obtainingthe first slot by using a hash function on the first key.
 13. Thecomputer-readable storage medium of claim 9, wherein the first datastructure and the second data structure together form a combined datastructure that represents the array, and wherein the combined datastructure includes a first pointer to the first data structure and asecond pointer to the second data structure.
 14. The computer-readablestorage medium of claim 9 further comprising: instructions for readingthe buckets of the second data structure contiguously to obtain anordered set of pointers; and instructions for reading a set of the slotspointed to by the ordered set of pointers to obtain the key-value pairsin the order in which the key-value pairs are inserted into the array.15. An apparatus, comprising: a processor; a data structure creationmodule configured to receive a request to create an array data structurefor implementing an array of a programming language, the array datastructure configured to store multiple key-value pairs; and animplementation selection module to select, based on a pre-definedselection criteria, one of multiple implementations of the array datastructure to generate a selected implementation, the implementationsincluding at least one of a first implementation and a secondimplementation, the first implementation storing the key-value pairs inthe order in which the key-value pairs are inserted into the array, thesecond implementation storing multiple pointers that are associated withthe key-value pairs in the order in which the key-value pairs areinserted into the array, wherein the data structure creation module isconfigured to create the array data structure based on the selectedimplementation.
 16. The apparatus of claim 15, wherein, in the firstimplementation, the data structure creation module is configured to:generate a first data structure having multiple contiguous buckets of amemory of a computer system, the buckets configured to store thekey-value pairs in the order in which the key-value pairs are insertedinto the array, and generate a second data structure having multipleslots of the memory, the slots configured to store the pointers, aspecified slot of the slots corresponding to a specified key of thekey-value pairs, the specified slot configured to store a specifiedpointer of the pointers that points to a specified bucket of the firstdata structure storing one of the key-value pairs having the specifiedkey, wherein the first data structure and the second data structure aredifferent data structures, wherein the first data structure and thesecond data structure together form a combined data structure thatrepresents the array, and wherein the combined data structure includes afirst pointer to the first data structure and a second pointer to thesecond data structure.
 17. The apparatus of claim 15, wherein, in thesecond implementation, the data structure creation module is configuredto: generate a third data structure having multiple slots of a memory ofa computer system, the slots configured to store the key-value pairs, aspecified slot of the slots corresponding to a specified key of thekey-value pairs, and generate a fourth data structure having multiplecontiguous buckets of the memory, the buckets configured to store thepointers, the pointers pointing to the slots storing the key-valuepairs, a specified pointer of the pointers associated with a specifiedkey-value pair of the key-value pairs, and the buckets furtherconfigured to store the pointers in the order in which the key-valuepairs are inserted into the array.
 18. The apparatus of claim 15,wherein the implementation selection module is configured to select thefirst implementation if at least one of (i) a frequency of fetchingvalues of the key-value pairs in the order they are inserted exceeds afirst threshold, or (ii) if a size of the array is below a secondthreshold.
 19. The apparatus of claim 15, wherein the implementationselection module is configured to select the second implementation if atleast one of (i) a frequency of fetching values of the key-value pairsin an order other than in which the key-value pairs are inserted exceedsa first threshold, (ii) a size of the array exceeds a second threshold,(iii) a number of reads/writes performed on the array exceeds a thirdthreshold, or (iv) a number of values read from or written into thearray exceeds a fourth threshold.
 20. The apparatus of claim 15, whereinthe pre-defined selection criteria includes at least one of (i) anaccess pattern of the array, or (ii) a size of the array.